<?php
/**
 * @desc 订单管理
 * @author kevin wang
 * @date 2014-09-20
 */
class ctrl_order extends ctrl_base {
    private static $dirName = 'order';
	private static $_visit_log_attr = array('序号','销售经理','购货单位','合同编号','产品名称','材质','规格','合同重量','合同回签时间','到款时间','计划到货时间','排产时间','下单重量','出厂重量','未下单重量','运输方式','送货地址','操作管理员','最后操作时间');
    /**
     * @desc 新闻列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('order_list'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$sales_customer = isset($_POST['sales_customer']) ? trim($_POST['sales_customer']) : '';
    	$sales_name = isset($_POST['sales_name'])? trim($_POST['sales_name']):'';
    	$agreement_no = isset($_POST['agreement_no'])? trim($_POST['agreement_no']):'';
		//$produce_id = isset($_POST['produce_id'])? intval($_POST['produce_id']):0;
		$produce_name = isset($_POST['produce_name'])? trim($_POST['produce_name']):'';
		$material = isset($_POST['material'])? trim($_POST['material']):'';
		$formatg = isset($_POST['formatg'])? trim($_POST['formatg']):'';
    	//搜索条件组合
		$condition = array();
    	if(!empty($sales_customer)) {
    		$condition['sales_customer'] = $sales_customer;
    	}
		if(!empty($sales_name)) {
    		$condition['sales_name'] = $sales_name;
    	}
		if(!empty($agreement_no)) {
    		$condition['agreement_no'] = $agreement_no;
    	}
		if(!empty($produce_name)) {
    		$condition['produce_name'] = $produce_name;
    	}
		if(!empty($material)) {
    		$condition['material'] = $material;
    	}
		if(!empty($formatg)) {
    		$condition['formatg'] = $formatg;
    	}
		//订单获取
    	$order_list = model_order :: orderList($nextpage,$pagesize,$condition);
    	$total = array_pop($order_list);
		$produceList = model_order :: getProduceList();
		//模板赋值
		self::smarty() -> assign('produceList', $produceList);
    	self::smarty() -> assign('order_list', $order_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('sales_customer', $sales_customer);
		self::smarty() -> assign('sales_name', $sales_name);
		self::smarty() -> assign('agreement_no', $agreement_no);
		self::smarty() -> assign('produce_name', $produce_name);
		self::smarty() -> assign('material', $material);
		self::smarty() -> assign('formatg', $formatg);
		self::smarty() -> assign('format', self::$_shoNum);

		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/order_list.html');    	
    }
	//添加订单
	public static function orderAdd(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('order_add'));
		if($_POST){

			$order = self::get_request_data();
			$order['author'] = $_SESSION['admin']['username'];
			$order['editer'] = $_SESSION['admin']['username'];
			//self::limit_weixin_id($order['sales_id']);
			$same = model_order::orderIsSame($order['agreement_no'], $order['produce_name'], $order['material'], $order['format']);
			if(!empty($same)){
				self::ajax_ouput(300, '根据录入的“合同编号、产品、材质、规格”信息，查询已存在相同订单记录，请在原记录上更新订单状态');
			}
			$result = model_order::orderAdd($order);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','manger_order','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}
		}
		//产品大分类
		/*$big_class = model_produce::get_type_list();
		self::smarty() -> assign("big_class", $big_class);
		//产品小分类
		$small_class = model_formula::get_small_class($big_class[0]['product_class_id']);
		self::smarty() -> assign("small_class", $small_class);*/
		self::smarty() -> display(self::$dirName . '/order_add.html');
	}
	//添加订单接口
	public static function interfaceAdd(){
		if($_POST){

			$order = self::get_request_data();
			$same = model_order::orderIsSame($order['agreement_no'], $order['produce_name'], $order['material'], $order['format']);
			if(!empty($same)){
				self::ajax_ouput(300, '根据录入的“合同编号、产品、材质、规格”信息，查询已存在相同订单记录，请在原记录上更新订单状态');
			}
			$result = model_order::orderAdd($order);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功');
			}else{
				self::ajax_ouput(300, '操作失败');
			}
		}
		self::ajax_ouput(300, '操作失败');
	}
	//删除订单
	public static function orderDelete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('order_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$id[] = intval($v);
		}
    	$data = array('delete_flag' => 1);

    	$st = model_order::updateOrderInfo($id, $data);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','manger_order');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
	//编辑订单
	 public static function orderEdit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$order = model_order::getOrderInfoById($id);
		$ctrl_tag = 'order_edit_other';
		if($order['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'order_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$order = array();

			$order = self::get_request_data();
			$order['editer'] = $_SESSION['admin']['username'];
			unset($order['id']);
			//self::limit_weixin_id($order['sales_id']);

			$result = model_order::updateOrderInfo(explode(',',$id), $order);
	    	
		    if(!empty($result)){
				self::ajax_ouput(200, '操作成功','manger_order','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}
		//产品大分类
		/*$big_class = model_produce::get_type_list();
		self::smarty() -> assign("big_class", $big_class);
		//产品小分类
		$small_class = model_formula::get_small_class($order['produce_class_id']);
		self::smarty() -> assign("small_class", $small_class);*/
    	self::smarty() -> assign('id', $id);
    	self::smarty() -> assign('order', $order);
    	self::smarty() -> display(self::$dirName . '/order_edit.html');
    }
	//高级搜索框
	public static function senior(){
		//产品大分类
		/*$big_class = model_produce::get_type_list();
		self::smarty() -> assign("big_class", $big_class);
		//产品小分类
		$small_class = model_formula::get_small_class($big_class[0]['product_class_id']);
		self::smarty() -> assign("small_class", $small_class);*/
		self::smarty() -> display(self::$dirName . '/senior_search.html');
	}
	//批量上传模板下载
	public static function get_betch_model(){
		self::download_xls_output(PATH_WWW.DOWNLOAD_FILES_PATH.'betch_add_order'.self::$_excel_pre);
	}
	//批量上传订单
	public static function batch(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('order_add'));
		//文件上传
		$info = self::file_upload_xls();
		//xls文件解析
		framework_base_autoloader::import_vendor('excel/PHPExcel.php');
		$objPHPExcel = new PHPExcel();
		$objPHPExcel = PHPExcel_IOFactory::load($info['url']);
		$orders = $objPHPExcel->getSheet(0)->toArray();
		//数据读取
		unset($orders[0]);
		foreach($orders as $key => $value){
			$len = count($value);
			$orders[$key][$len] = $_SESSION['admin']['username'];
			$orders[$key][$len + 1] = $_SESSION['admin']['username'];
		}
		$result = model_order::batch_insert_order($orders);
		if(!empty($result)){
			self::ajax_ouput(200, '操作成功','manger_order','closeCurrent');
		}else{
			self::ajax_ouput(300, '操作失败,请重新上传');
		}
	}
	//导出订单记录
	public static function orderExport(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		//self::operation(array('order_export'));
    	//页面输入参数处理
    	$currentPage = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
    	$pagesize = isset($_GET['numPerPage']) ? $_GET['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$sales_customer = isset($_REQUEST['sales_customer']) ? trim($_REQUEST['sales_customer']) : '';
    	$sales_name = isset($_REQUEST['sales_name'])? trim($_REQUEST['sales_name']):'';
    	$agreement_no = isset($_REQUEST['agreement_no'])? trim($_REQUEST['agreement_no']):'';
		//$produce_id = isset($_POST['produce_id'])? intval($_POST['produce_id']):0;
		$produce_name = isset($_REQUEST['produce_name'])? trim($_POST['produce_name']):'';
		$material = isset($_REQUEST['material'])? trim($_REQUEST['material']):'';
		$formatg = isset($_REQUEST['formatg'])? trim($_REQUEST['formatg']):'';
    	//搜索条件组合
		$condition = array();
    	if(!empty($sales_customer)) {
    		$condition['sales_customer'] = $sales_customer;
    	}
		if(!empty($sales_name)) {
    		$condition['sales_name'] = $sales_name;
    	}
		if(!empty($agreement_no)) {
    		$condition['agreement_no'] = $agreement_no;
    	}
		if(!empty($produce_name)) {
    		$condition['produce_name'] = $produce_name;
    	}
		if(!empty($material)) {
    		$condition['material'] = $material;
    	}
		if(!empty($formatg)) {
    		$condition['formatg'] = $formatg;
    	}
		//订单获取
    	$order_list = model_order :: orderList($nextpage,$pagesize,$condition);
		$produceList = model_order :: getProduceList();
    	$total = array_pop($order_list);
		array_unshift($order_list,self::$_visit_log_attr);
		//载入xls处理类
		$time = time();
		$title = "订单数据记录";
		$subject = "order log-{$_SESSION['admin']['username']}-{$time}";
		$descript = "order log-{$_SESSION['admin']['username']}-{$time}";
		$username = $_SESSION['admin']['username'];
		framework_base_autoloader::import_vendor('excel/PHPExcel.php');
		$objPHPExcel = new PHPExcel();
		//设置属性
		$objPHPExcel->getProperties()->setCreator($username);
		$objPHPExcel->getProperties()->setLastModifiedBy($username);
		$objPHPExcel->getProperties()->setTitle($title);
		$objPHPExcel->getProperties()->setSubject($subject);
		$objPHPExcel->getProperties()->setDescription($descript);
		//添加数据
		$objPHPExcel->setActiveSheetIndex(0);
		$cellFirst = 65;
		foreach($order_list as $keys => $values){
			$i = 0;
			foreach($values as $k=>$v){
				//if($k == 'order_id')continue;
				$parentKey = chr($cellFirst + $i);
				$cellKey = $parentKey.($keys+1);
				$cellVlue = $order_list[$keys][$k];
				/*if($keys != 0 && $k == 'cooperation_status'){
					$cellVlue = ($cellVlue == 1)?'合作中':($v == 2?'合作中断':'无合作历史');
				}
				if($keys != 0 && $k == 'status'){
					$cellVlue = ($cellVlue == 0)?'审核中':'已审核';
				}*/
				/*if($keys != 0 && $k == 'produce_id'){
					$cellVlue = $produceList[$cellVlue];
				}*/
				$objPHPExcel->getActiveSheet()->SetCellValue($cellKey, $cellVlue);
				$i++;
			}
		}
		// Rename sheet
		$objPHPExcel->getActiveSheet()->setTitle($title);
		switch(self::$_excel_pre){
			case '.xls':$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);break;
			case '.xlsx':$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);break;//Save Excel 2007 file
			default:$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);break;
		}		 
		$down = self::get_down_file_url(self::$_excel_pre);
		$objWriter->save($down['save']);
		self::download_xls_output($down['save'], $down['down']); 
	}
}
